//
//  HomeViewController.swift
//  GifUsage
//
//  Created by han on 2020/12/21.
//  Copyright © 2020 Ting Han. All rights reserved.
//

import UIKit

class HomeViewController: UITableViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
}

extension HomeViewController {
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        switch indexPath.section {
        case 0:
            switch indexPath.row {
            case 0: // 我的相册
                // performSegue(withIdentifier: "AlbumStoryboard", sender: nil)
                let home =  UIStoryboard(name: "Album", bundle: nil).instantiateViewController(identifier: "AlbumHomeViewController") as! AlbumHomeViewController
                navigationController?.pushViewController(home, animated: true)
                
            case 1:
                pushToAlbumList()
            case 2: // 我的相册
                pushToAlbum()
            case 3: // 我的照片
                pushToAlbum(.image)
            case 4:
                pushToAlbum(.video)
            case 5:
                pushToAlbum(.livePhoto)
            case 6:
                pushToAlbum(.burst)
            case 7:
                pushToAlbum(.gif)
            default:
                break
            }
        case 1:
            switch indexPath.row {
            case 0:
                pushToEdit(.imageToGIF)
            case 1:
                pushToEdit(.videoToGIF)
            case 2:
                pushToEdit(.livePhotoToGIF)
            case 3:
                pushToEdit(.burstToGIF)
            case 4:
                pushToEdit(.editGIF)
            case 5:
                pushToEdit(.gifToImage)
            case 6: // livePhoto功能
                performSegue(withIdentifier: "livePhoto", sender: nil)
            default:
                break
            }
        default:
            break
        }
        
    }
    
    func pushToAlbumList() {
        func pushAction(_ result: Bool?) {
            if result == false {
                print("没有权限")
                return
            } else if result == true {
               performSegue(withIdentifier: "albumList", sender: nil)
            }
        }
        
        let result = GIFPickerManager.shared.isAuthorized { (result) in
            pushAction(result)
        }
        pushAction(result)
    }
    
    func pushToAlbum(_ assetType: AssetCollectionType? = nil) {
        func pushAction(_ result: Bool?) {
            if result == false {
                print("没有权限")
                return
            } else if result == true {
                let grid = AssetGridViewController()
                if let assetType = assetType {
                    let result = GIFPickerManager.shared.fetchAsset(assetType)
                    grid.fetchResult = result
                }
                navigationController?.pushViewController(grid, animated: true)
            }
        }
        
        let result = GIFPickerManager.shared.isAuthorized { (result) in
            pushAction(result)
        }
        pushAction(result)
    }
    
    func pushToEdit(_ functionType: FunctionType) {
        func pushAction(_ result: Bool?) {
            if result == false {
                print("没有权限")
                return
            } else if result == true {
                let asset = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(identifier: String(describing: AssetCollectionViewController.self)) as! AssetCollectionViewController
                let results = GIFPickerManager.shared.fetchAsset(functionType.assetType)
                asset.fetchResult = results
                asset.functionType = functionType
                asset.title = functionType.title
                navigationController?.pushViewController(asset, animated: true)
            }
        }
        
        let result = GIFPickerManager.shared.isAuthorized { (result) in
            pushAction(result)
        }
        pushAction(result)
    }
}
